﻿package jlib.component.tool.skin.points.decorator {
	
	import jlib.component.tool.skin.points.AbstractControlPoint;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 * @author �����
	 */
	public class Decorator extends AbstractControlPoint 
	{
		protected var m_mode:String;
		
		protected var m_skin:DisplayObject;
		protected var m_decorator:Decorator;
		protected var m_cursor:DisplayObject;
		
		public function Decorator($skin:DisplayObject=null, $cursor:DisplayObject=null): void
		{
			this.doubleClickEnabled = true;
			if($skin) setSkin($skin);
			if($cursor){
				m_cursor = $cursor;
				m_cursor.visible = false;
			} 
			
			if(m_decorator) {
				m_decorator = m_decorator;
				addChild(m_decorator);
			}
			if(!stage) this.addEventListener(Event.ADDED_TO_STAGE , initialize);
			else initialize();
		}
		
		private function initialize( e:Event = null ): void
		{
			this.addEventListener( Event.REMOVED_FROM_STAGE, onRemovedDisplay );
			addEvent();
		}
		
		private function onRemovedDisplay(e:Event):void
		{
			this.removeEventListener( MouseEvent.MOUSE_OVER, onShowCursor );
			this.removeEventListener( MouseEvent.MOUSE_OUT, onHideCursor );
			stage.removeEventListener( MouseEvent.MOUSE_MOVE, onMouseMoves );
			stage.removeEventListener(MouseEvent.MOUSE_DOWN , onMouseDowns );
			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUps );
		}
		
		private function addEvent(e:Event=null): void
		{
			this.addEventListener( MouseEvent.MOUSE_OVER, onShowCursor );
			this.addEventListener( MouseEvent.MOUSE_OUT, onHideCursor );
			stage.addEventListener( MouseEvent.MOUSE_MOVE, onMouseMoves );
			stage.addEventListener(MouseEvent.MOUSE_DOWN , onMouseDowns );
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUps );
		}
		
		private function onMouseDowns(e: MouseEvent): void
		{
			if( e.target == this ){
				if(cursor) cursor.visible = true;
				this.removeEventListener( MouseEvent.MOUSE_OVER, onShowCursor );
				this.removeEventListener( MouseEvent.MOUSE_OUT, onHideCursor );
			}
		}
		
		private function onMouseUps(e: MouseEvent): void
		{
			if(cursor) cursor.visible = false;
			this.addEventListener( MouseEvent.MOUSE_OVER, onShowCursor );
			this.addEventListener( MouseEvent.MOUSE_OUT, onHideCursor );
		}
		
		private function onMouseMoves(e:MouseEvent): void
		{
			// var rect:Rectangle = this.getRect(stage);
			if(!stage) return ;
			if(cursor) {
				cursor.x = stage.mouseX;
				cursor.y = stage.mouseY;	
			}
		}
		
		private function onHideCursor(e:MouseEvent): void
		{
			if(!cursor) return ;
			this.addChild(cursor);
			cursor.visible = false;
		}
		
		private function onShowCursor(e:MouseEvent): void
		{
			if(!stage) return ;
			if(!cursor) return ;
			stage.addChild(cursor);
			cursor.visible = true;
		}
		
		public function setSkin( skin:DisplayObject ): void 
		{ 
			this.m_skin = skin; 
			this.addChildAt(skin,0);
		}
		
		public function get cursor(): DisplayObject { return m_cursor; }
		public function set cursor( $cursor:DisplayObject ): void 
		{ 
			if($cursor == null) return ;
			m_cursor = $cursor;
			addChild(m_cursor);
		}
		
		override public function getMode(): String { return m_mode; }
		
		override public function setMode($value:String): void {
			m_mode = $value;
			if(m_decorator) m_decorator.setMode(m_mode);
		}
		
		override public function dispose(): void {
			if(m_decorator) m_decorator.dispose();
			if(parent.contains(this)) parent.removeChild(this);
		}
		
	}//c
}
